import axios from "axios";
import { getToken } from "@/util/auth";

const server = axios.create({
  baseURL: import.meta.env.VITE_BASE_URL,
  timeout: 5000,
});

server.interceptors.request.use(
  (config) => {
    // 请求头加token
    const token = getToken();
    if (token && config && config.headers) {
      config.headers.token = token;
    }
    return config;
  },
  (err) => {
    console.log("request 错误");
    return Promise.reject(err);
  }
);
server.interceptors.response.use(
  (res) => {
    return res;
  },
  (err) => {
    console.log("response 错误", err);
    return Promise.reject(err);
  }
);

export default server;
